package com.leyou.item.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.item.entity.Category;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>implements CategoryService {

//    @Autowired
//    CategoryService categoryService;
    @Autowired
    CategoryBrandService categoryBrandService;
    @Override
    public List<CategoryDTO> queryCategoryByParentId(Long pid) {
        List<Category> categories = query().eq("parent_id", pid).list();
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);
        return categoryDTOS;
    }

    @Override
    public CategoryDTO queryCategoryById(Long id) {

        return new CategoryDTO(this.getById(id));
    }

    @Override
    public List<CategoryDTO> listCategoryByIds(List<Long> ids) {

        return CategoryDTO.convertEntityList(listByIds(ids));
    }

    @Override
    public List<CategoryDTO> queryCategoryByBrandId(Long bid) {
/*        List<CategoryBrand> brandList = this.categoryBrandService.query().eq("brand_id", bid).list();
        List<Long> collect = brandList.stream().map(CategoryBrand::getBrandId).collect(Collectors.toList());
//        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(this.listByIds(collect));
//
//        return categoryDTOS;\\



       // List<Category> categories = this.listByIds(collect);

        List<Category> categories = this.listByIds(collect);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);
        System.out.println(categoryDTOS.size()+"11111111111");*/

        List<CategoryBrand> lists = categoryBrandService.query().eq("brand_id", bid).list();

       List<Long> cids = new ArrayList<>();

        for (CategoryBrand list : lists) {
            Long categoryId = list.getCategoryId();
            cids.add(categoryId);
            System.out.println("categoryId = " + categoryId);
        }
        List<Category> categories = this.listByIds(cids);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);


        return categoryDTOS;
    }
}
